package com.sj.uabrowser.mvp.video.loader;

import com.orhanobut.logger.Logger;
import com.sj.uabrowser.framework.BaseLoader;
import com.sj.uabrowser.framework.BasePresenter;
import com.sj.uabrowser.mvp.video.bean.NVideoItem;
import com.sj.uabrowser.mvp.video.bean.VideoNav;
import com.sj.uabrowser.net.AuthResultClient;
import com.sj.uabrowser.net.ServiceApi;
import com.sj.uabrowser.net.bean.NewUrl;

import java.util.List;
import java.util.TreeMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class VideoListLoader extends BaseLoader {

    public VideoListLoader(BasePresenter mPresenter) {
        super(mPresenter);
    }

    public void fetchTabVideoList(final int requestId, TreeMap<String,Object> params) {
        AuthResultClient.getServiceVideo(ServiceApi.class)
                .TabVideoList(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object mainFunc) throws Exception {
                        if(mPresenter!=null)
                            mPresenter.onDataCallBack(requestId, mainFunc);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        if(mPresenter!=null)
                            mPresenter.onDataCallBackError(requestId, throwable);
                    }
                });
    }

    public void fetchVideoList(final int requestId, TreeMap<String,Object> params) {
        AuthResultClient.getServiceVideo(ServiceApi.class)
                .VideoList(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<List<NVideoItem>>() {
                    @Override
                    public void accept(List<NVideoItem> mainFunc) throws Exception {
                        if(mPresenter!=null)
                            mPresenter.onDataCallBack(requestId, mainFunc);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        if(mPresenter!=null)
                            mPresenter.onDataCallBackError(requestId, throwable);
                    }
                });
    }


    public void refreshVideoList(final int requestId, TreeMap<String,Object> params) {
        AuthResultClient.getServiceVideo(ServiceApi.class)
                .VideoList(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<List<NVideoItem>>() {
                    @Override
                    public void accept(List<NVideoItem> mainFunc) throws Exception {
                        if(mPresenter!=null)
                            mPresenter.onDataCallBack(requestId, mainFunc);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        if(mPresenter!=null)
                            mPresenter.onDataCallBackError(requestId, throwable);
                    }
                });
    }

    public void fetchVideoApars(final int requestId, TreeMap<String,Object> params) {
        AuthResultClient.getServiceVideo(ServiceApi.class)
                .VideoApars(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<NewUrl>() {
                    @Override
                    public void accept(NewUrl mainFunc) throws Exception {
                        Logger.e("newUrl:"+mainFunc.getUrl());
                        if(mPresenter!=null) {
                            mPresenter.onDataCallBack(requestId, mainFunc);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        if(mPresenter!=null) {
                            mPresenter.onDataCallBackError(requestId, throwable);
                        }
                    }
                });
    }

}
